import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller.dart';

/// 底部导航栏
/// 
/// 原生样式的底部导航栏组件
/// 支持点击反馈和选中状态展示
class FloatingBottomNavigationBar extends GetView<MainController> {
  const FloatingBottomNavigationBar({super.key});

  /// 导航项数据配置
  /// 包含图片图标路径、选中图标路径、标签信息
  static const List<Map<String, String>> _navItems = [
    {'icon': 'assets/images/select_homepage_no.png', 'activeIcon': 'assets/images/select_homepage_yes.png', 'label': '首页'},
    {'icon': 'assets/images/select_play_no.png', 'activeIcon': 'assets/images/select_play_yes.png', 'label': '一起玩'},
    {'icon': 'assets/images/select_publish_no.png', 'activeIcon': 'assets/images/select_publish_yes.png', 'label': '发布'},
    {'icon': 'assets/images/select_mine_no.png', 'activeIcon': 'assets/images/select_mine_yes.png', 'label': '我的'},
    {'icon': 'assets/images/select_ai_no.png', 'activeIcon': 'assets/images/select_ai_yes.png', 'label': 'AI'},
  ];

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return _buildNavigationContainer();
    });
  }

  /// 构建导航栏容器
  Widget _buildNavigationContainer() {
    return Container(
      height: 80, // Flutter Material 3 标准高度（带图标+文字）
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.08),
            blurRadius: 8,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: SafeArea(
        top: false,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: _buildNavigationItems(),
        ),
      ),
    );
  }

  /// 构建所有导航项
  List<Widget> _buildNavigationItems() {
    return _navItems.asMap().entries.map((entry) {
      final index = entry.key;
      final item = entry.value;
      final isSelected = controller.currentIndex.value == index;

      return _buildNavigationItem(
        index: index,
        item: item,
        isSelected: isSelected,
      );
    }).toList();
  }

  /// 构建单个导航项
  Widget _buildNavigationItem({
    required int index,
    required Map<String, String> item,
    required bool isSelected,
  }) {
    // 🎤 特殊处理：AI导航项（索引4）支持长按录音
    if (index == 4) {
      return Expanded(
        child: GestureDetector(
          onTap: () => controller.onBottomNavTap(index),
          // 🎤 长按开始录音（只有在AI页面激活时才响应）
          onLongPressStart: (details) {
            if (controller.currentIndex.value == 4) {
              controller.onAiIconLongPressStart();
            }
          },
          // 🎤 长按结束停止录音
          onLongPressEnd: (details) {
            if (controller.currentIndex.value == 4) {
              controller.onAiIconLongPressEnd();
            }
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildAnimatedIcon(item, isSelected),
              const SizedBox(height: 2),
              _buildAnimatedLabel(item, isSelected),
            ],
          ),
        ),
      );
    }
    
    // 所有导航项统一处理
    return Expanded(
      child: InkWell(
        onTap: () => controller.onBottomNavTap(index),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildAnimatedIcon(item, isSelected),
            const SizedBox(height: 2),
            _buildAnimatedLabel(item, isSelected),
          ],
        ),
      ),
    );
  }

  /// 构建图标（使用图片）
  Widget _buildAnimatedIcon(
    Map<String, String> item, 
    bool isSelected,
  ) {
    return Image.asset(
      isSelected 
          ? item['activeIcon']! 
          : item['icon']!,
      width: 24,
      height: 24,
      fit: BoxFit.contain,
    );
  }

  /// 构建标签
  Widget _buildAnimatedLabel(
    Map<String, String> item, 
    bool isSelected,
  ) {
    return Text(
      item['label']!,
      style: TextStyle(
        fontSize: 10,
        fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
        color: isSelected 
            ? Colors.blue.shade700 
            : Colors.grey.shade600,
      ),
    );
  }
}
